package cn.farm.farm.Controller.OrdinaryController;


import cn.farm.farm.Aop.IsLogin;
import cn.farm.farm.Entity.Ordinary.Information;
import cn.farm.farm.Entity.Ordinary.InformationSon;
import cn.farm.farm.MessageModel.Common;
import cn.farm.farm.MessageModel.MessageList;
import cn.farm.farm.Service.OridinaryService.ImageService;
import cn.farm.farm.Service.OridinaryService.InformationService;
import cn.farm.farm.Service.OridinaryService.MatchImageService;
import cn.farm.farm.Service.OridinaryService.VideoService;
import cn.farm.farm.Util.ImageUtil;
import cn.farm.farm.Util.UserUtil;
import cn.farm.farm.Util.VideoUtil;
import cn.farm.farm.Util.file.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/farm/ordinary")
public class InformationController {

    @Autowired
    VideoUtil<Information> videoUtil;

    @Autowired
    ImageUtil<Information> imageUtil;

    @Autowired
    ImageService imageService;

    @Autowired
    MatchImageService matchImageService;

    @Autowired
    VideoService videoService;

    @Autowired
    InformationService informationService;

    @IsLogin
    @PostMapping(value = "info/insert")
    public Common addInfomation(HttpSession session , String title, String content , String location ,
                                @RequestParam(required = false,value ="image") List<MultipartFile> imageList,
                                @RequestParam(required = false,value ="video") MultipartFile video){
        try {
            int author = UserUtil.getUserID(session);
            Information information = new Information(title,content,author,location);
            List<String> ImagePathList = OssUtil.dealImage(imageList);//将图片表转化为图片地址集合
            if (video == null || video.equals("") || video.getSize() <= 0) {
                return new Common(401,"视频未上传");
            }
            //转化video为视频地址
            String VedioPath = OssUtil.uploadVideo(video);
            //1.将图片插入表中并且返回imageID并且插入到match_image中
            //name format(图片格式) description(图片描述) author time location device(上传设备)(不做要求)
            // Image image;

            Integer matchId = imageService.insertImage(title,location,author,content,ImagePathList);
            //图片已经插入完成

            Integer videoId = imageService.insertVideo(title,location,author,content,VedioPath);

            informationService.AddInformation(title,content,author,location,matchId,videoId);

        }catch (Exception e){
            log.error("addInfomation出错:"+e);
            return new Common(405, "server error");
        }
        return new Common(200,"ok");
    }

    @IsLogin
    @PostMapping(value = "info/all")
    public Common selectMyInfomation(HttpSession session,
                                @RequestParam(value ="pageNum") Integer pageNum,
                                @RequestParam(value ="pageSize") Integer pageSize
    ){
        int author = UserUtil.getUserID(session);
        MessageList<List<InformationSon>> m = informationService.foundAll(author,pageNum,pageSize);
        m.setCode(200);
        m.setMessage("ok");
        return m;
    }
}
